package com.daxt.controller.warehouse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daxt.common.result.PageInfo;
import com.daxt.common.result.PageUtil;
import com.daxt.common.result.Result;
import com.daxt.common.result.ResultCode;
import com.daxt.common.result.ResultUtil;
import com.daxt.mapper.warehouse.WarehouseTestingMapper;
import com.daxt.model.service.warehouse.param.QueryTime;
import com.daxt.model.service.warehouse.vo.Warehouse;
import com.daxt.model.service.warehouse.vo.WarehouseTesting;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "库房温度")
@RestController
@RequestMapping("/warehouse/testing")
public class WarehouseTestingController {

	@Autowired
	private WarehouseTestingMapper warehouseTestingMapper;
	
	@ApiOperation(value = "库房温度列表", notes = "") 
	@GetMapping("/list") 
	public Result<PageInfo<WarehouseTesting>> list(@ApiParam("") @RequestParam(value = "pageNum", required = false) Integer pageNum,
			@ApiParam("") @RequestParam(value = "pageSize", required = false) Integer pageSize){
		Page<WarehouseTesting> page = new Page<>(pageNum == null ? 0 : pageNum,
				pageSize == null ? 10 : pageSize);
		QueryWrapper<WarehouseTesting> queryWrapper=new QueryWrapper<WarehouseTesting>();
		queryWrapper.orderByDesc("createTime");
		 IPage<WarehouseTesting> info = warehouseTestingMapper.selectPage(page, queryWrapper);
		 PageInfo<WarehouseTesting> data = PageUtil.data(info.getPages(), info.getCurrent(), info.getTotal(),
					info.getRecords());
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, data);
	}
	
	@ApiOperation(value = "库房温度统计", notes = "") 
	@PostMapping("/statistics") 
	public Result<Map<String,List<String>>> list(@RequestBody QueryTime queryTime){
		QueryWrapper<WarehouseTesting> queryWrapper=new QueryWrapper<WarehouseTesting>();
		queryWrapper.between("createTime",queryTime.getStartTime(), queryTime.getEndTime());
		List<WarehouseTesting> list = warehouseTestingMapper.selectList(queryWrapper);
		Map<String,List<String>> result=new HashMap<String, List<String>>();
		List<String> title=new ArrayList<String>();
		List<String> value1=new ArrayList<>();
		List<String> value2=new ArrayList<>();
		if(list!=null&&list.size()>0) {
			for (WarehouseTesting warehouseTesting : list) {
				title.add(warehouseTesting.getCreateTime().toString());
				value1.add(warehouseTesting.getTemperature());
				value2.add(warehouseTesting.getHumidity());
			}
		}
		result.put("title",title);
		result.put("value1",value1);
		result.put("value2",value2);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, result);
	}
	
	
	@ApiOperation(value = "库房温度添加", notes = "") 
	@PostMapping("") 
	public Result<String> save(@RequestBody WarehouseTesting entity){
		//entity.setCreateTime(LocalDateTime.now());
		warehouseTestingMapper.insert(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}
	
	@ApiOperation(value = "库房温度修改", notes = "") 
	@PutMapping("") 
	public Result<List<Warehouse>> update(@RequestBody WarehouseTesting entity){
		warehouseTestingMapper.updateById(entity);
		return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), null, null);
	}

	
	@ApiOperation(value = "库房温度删除", notes = "") 
	@DeleteMapping("") 
	public Result<List<String>> del(@RequestBody List<String> ids){
		if(ids!=null&&ids.size()>0) {
			warehouseTestingMapper.deleteBatchIds(ids);
			return ResultUtil.data(ResultCode.RESULT_SUCCESS.getCode(), "", null);
		}else {
			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "删除至少选择一个库房温度！！！", null);
		}
	}
	

}
